<?php

class ItemlistingController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $request = $this->getRequest();
        
        $dbConn = new Application_Model_DbTable_PricelistMasterItems();
        
        $sqlQuery = $dbConn->select()
        					->from(array('p' => 'tbl_pricelistMasterItems'))
        					->columns(array(
        									'id' => 'id',
        									'pricelistDesc' => 'pricelistDesc',
        									'inventoryItem' => 'inventoryItem',
        									'listedOnDate' => 'listedOnDate',
        									'hek' => new Zend_Db_Expr("CASE WHEN p.hekCH=null THEN p.hek ELSE p.hekCH END")))
        					->where('active=\'N\'');
        
        $result = $sqlQuery->query()->fetchAll();
        
        $this->view->entries = $result;
    }

    public function changeindexAction()
    {
    	$request = $this->getRequest();
    	
    	$db = Zend_Db_Table::getDefaultAdapter();
    	
    	if($request->getParam('active') != null) {
    		$sql = $db->select()
			    		->from(array('p' => 'tbl_pricelistMasterItems'))
			    		->columns(array(
			    				'id' => 'id',
			    				'pricelistDesc' => 'pricelistDesc',
			    				'inventoryItem' => 'inventoryItem',
			    				'listedOnDate' => 'listedOnDate',
			    				'hek' => new Zend_Db_Expr("CASE WHEN p.hekCH=null THEN p.hek ELSE p.hekCH END")))
			    				->where('active=\''.$request->getParam('active').'\'');
    	} else if ($request->getParam('sku') != null) {
    		$sql = $db->select()
			    		->from(array('p' => 'tbl_pricelistMasterItems'))
			    		->columns(array(
			    				'id' => 'id',
			    				'pricelistDesc' => 'pricelistDesc',
			    				'inventoryItem' => 'inventoryItem',
			    				'listedOnDate' => 'listedOnDate',
			    				'hek' => new Zend_Db_Expr("CASE WHEN p.hekCH=null THEN p.hek ELSE p.hekCH END")))
			    				->where('inventoryItem=\''.$request->getParam('sku').'\'');
    	}  else  {	
    		$sql = $db->select()
    		->from(array('p' => 'tbl_pricelistMasterItems'))
    		->columns(array(
    				'id' => 'id',
    				'pricelistDesc' => 'pricelistDesc',
    				'inventoryItem' => 'inventoryItem',
    				'listedOnDate' => 'listedOnDate',
    				'hek' => new Zend_Db_Expr("CASE WHEN p.hekCH=null THEN p.hek ELSE p.hekCH END")));
    	}
    	
    	
        $result = $sql->query()->fetchAll();
    	
    	$this->_helper->json($result);
    }

    public function addAction()
    {
        $form = new Application_Form_ItemlistingAdd();
        
        $request = $this->getRequest();
         
        $this->view->pageTitle = 'Add Item for Pricelist';
        
        if($request->isPost()) {
        	if ($form->isValid($request->getPost())) {
        		$sql = Zend_Db_Table::getDefaultAdapter();
        		
        		//insert liting info
         		try {
         			$sql->insert('tbl_pricelistMasterItems',$form->getValues());
          		} catch (Exception $e) {
          			echo $e->getMessage();
          			
          		}
          		//get last inserted ID
          		$listingId = $sql->lastInsertId('tbl_pricelistMasterItems', 'id');
          		
          		//insert pricing info
          		try {
          			$sql->insert('bis.tbl_pricing', array('listing_id' => $listingId, 
          													'hek' => $form->getValue('hek'), 
          													'hekCH' => $form->getValue('hekCH'),
          													'validFrom' => $form->getValue('listedOnDate'),
          													'srp' => $form->getValue('srp'),
          													'comment' => 'Release'));
          		} catch(Exception $e) {
          			echo $e->getMessage();
          		}
          		
          		//forward to customer allocation page
        		$this->_forward('customerallocate', 'itemlisting', null, array('id' => $listingId));
        	}
        }
        
        
        $this->view->form = $form;
    }

    public function getiteminfoAction()
    {
     	// get submitted item
     	$request = $this->getRequest();
     	
     	$db = new Application_Model_DbTable_inventoryItem();
     	
     	if ($request->getParam('inventoryItem') != null) {
     		$item = $request->getParam('inventoryItem');
     		
     		$sqlQuery = $db->select()
     							->from('tbl_inventoryItem')
     							->columns(array('*', '(releaseDate) - interval 42 day AS lastOrderDate'))
     							->where('inventoryItem=\''.$item.'\'');
     	} else {
     		$item = $request->getParam('eanCode');
     		
     		$sqlQuery = $db->select()
     							->from('tbl_inventoryItem')
     							->columns(array('*', '(releaseDate) - interval 42 day AS lastOrderDate'))
     							->where('eanCode=\''.$item.'\'');
     	}
     	
     	
     	
     	
     	$result = $sqlQuery->query()->fetch();
     	
     	if($result == null) 
     	{
     		$this->_helper->json(array('inventoryItemDesc' => 'item not found!'));
     	} else {
     		$this->_helper->json($result);
     	}
     	
    }

    public function editAction()
    {
    	$request = $this->getRequest();
    	$listId = $request->getParam('id');
    	
    	$db = new Application_Model_DbTable_PricelistMasterItems();
    	
    	$form = new Application_Form_ItemlistingEdit();
    	
    	if($request->isPost()) {
    		if($form->isValid($request->getPost())) {
		
    			
    		$sql = $db->update($form->getValues(), 'id='.$form->getValue('id'));
    		
    		
    			
    		$this->_redirect('/itemlisting/index');
    		}
    	}
    	    	
    	$sql = $db->select()
    				->setIntegrityCheck(false)
    				->from(array('p' => 'bis.tbl_pricelistMasterItems'))
    				->where('id='.$listId)
    				->joinLeft(array('i' => 'bis.tbl_inventoryItem'),'p.inventoryItem = i.inventoryItem', 'inventoryItemDesc'); 
    	$result = $sql->query()->fetch();
    	
    	$form->populate($result);
    	
    	$this->view->form = $form;
    	$this->view->pageTitle = 'Edit Itempricing';
    	
    	
    }

    public function customerallocateAction()
    {
        $form = new Application_Form_ItemlistingCustomerallocate();
        
        $request = $this->getRequest();
        
        $listingId = $request->getParam('id');
        
        $this->view->pageTitle = 'Allocate item to customers';
        $this->view->form = $form;
    }


}







